/**
 * @param {number[][]} matrix
 * @return {number[]}
 */

var spiralOrder = function (matrix) {
    let row = matrix.length
    let col = matrix[0].length
    let size = Math.floor(Math.min(row, col) + 1) / 2
    let res = []
    for (let i = 0; i < size; i++) {
        // 上遍历 1 2 3 4
        for (let j = i; j < col - i; j++) {
            res.push(matrix[i][j])
            console.log(matrix[i][j], "1")
        }
        // 右遍历 8 12
        for (let j = i + 1; j < row - i; j++) {
            res.push(matrix[j][col - 1 - i])
            console.log(matrix[j][col - 1 - i], "2")
        }
        // 下遍历 11 10 9
        for (let j = col - 2 - i; j >= i && row - 1 - i > i; j--) {
            res.push(matrix[row - 1 - i][j])
            console.log(matrix[row - 1 - i][j], "3")
        }
        // 左遍历 5
        for (let j = row - 2 - i; j > i && col - 1 - i > i; j--) {
            res.push(matrix[j][i])
            console.log(matrix[j][i], "4")
        }
    }
    return res
};
console.log(spiralOrder([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]))